package com.gxa.worker.recycling.service.impl;

import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.*;
import com.gxa.worker.recycling.mapper.*;
import com.gxa.worker.recycling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ：looker
 * @date ：Created in 2021/12/2 10:45
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RecyclerMapper recyclerMapper;

    @Autowired
    CommunityAllocateMapper allocateMapper;

    @Autowired
    ServiceAddressMapper serviceAddressMapper;

    @Autowired
    RecycleorderMapper recycleorderMapper;

    @Autowired
    GoodsorderMapper goodsorderMapper;

    @Autowired
    CommunityManageMapper communityManageMapper;

    @Override
    public List<Map<String,Object>> getUserByPositionOrPhone(Long id, String likeWord) throws BusinessException {
        List<Map<String,Object>> list = new ArrayList<>();
        //查出回收员
        Recycler recycler = recyclerMapper.selectByPrimaryKey(id);
        //获取回收员隶属小区id
        Example example = new Example(CommnuityAllocate.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wasterId",id);
        List<CommnuityAllocate> commnuityAllocates = allocateMapper.selectByExample(example);
        if(commnuityAllocates.size()<0){
            throw new BusinessException(ExceptionCode.WASTER_ERROE);
        }
        //小区id
        Long commnuityId = commnuityAllocates.get(0).getCommnuityId();
        //通过小区id，查询该小区临创用户
        List<User> users = userMapper.getUserByPositionOrPhone(commnuityId, likeWord);
        for (int i = 0; i < users.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("user",users.get(i));
            map.put("community",communityManageMapper.selectByPrimaryKey(users.get(i).getQuartersId()));
            list.add(map);
        }
        return list;
    }

    @Override
    public Map getOrdersById(Long id) {
        HashMap<String, Object> map = new HashMap<>(16);

        User user = userMapper.selectByPrimaryKey(id);
        map.put("user",user);
        Example example = new Example(Recycleorder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid",id);

        List<Recycleorder> recycleorders = recycleorderMapper.selectByExample(example);
        map.put("recycleorders",recycleorders);


        List<Map<String, Object>> littleGoodsOrderByUserId = goodsorderMapper.getLittleGoodsOrderByUserId(id);
        map.put("goodsorders",littleGoodsOrderByUserId);


        return map;
    }

    @Override
    public Map getUserById(Long id) {
        HashMap<String, Object> map = new HashMap<>(16);
        User user = userMapper.selectByPrimaryKey(id);
        map.put("user",user);
        CommunityManage communityManage = communityManageMapper.selectByPrimaryKey(user.getQuartersId());
        map.put("community",communityManage);
        return map;
    }

    @Override
    public void createUser(User user) {
        user.setType("临创用户");
        user.setLastLogin(new Date());
        user.setAllPoints(0);
        user.setNowPoints(0);
        user.setDays(0);
        user.setLevelId("VIP1");
        user.setState(false);
        int i = userMapper.insertSelective(user);
    }
}
